package com.mj.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
 * @description java实现仿百度文库
 * 1.安装OpenOffice 并开启OpenOffice服务 dos下 C:\Program Files (x86)\OpenOffice 4\program>soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
 * 2.加入JodConverter OpenOffice+JodConverter可以将Txt/Word/Excel/PPT转为PDF
 * 3.安装Swftools 这个可以将pdf转化为swf
 * 4.FlexPaper读取swf
 * 访问http://localhost:8080/wenku/convertUtil即可 
 * 能跑通，代码比较简陋 待后期优化
 * @author zhaominglei
 * @date 2015年8月1日
 */
public class ConvertServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private File sourceFile;//转换源文件
	private File pdfFile;//PDF目标文件
	private File swfFile;//SWF目标文件
	private Runtime r;				
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println(request.getServletContext().getRealPath("file/1.ppt"));
		sourceFile = new File(request.getServletContext().getRealPath("file/1.ppt"));
		pdfFile = new File(request.getServletContext().getRealPath("file/1.pdf"));
		swfFile = new File(request.getServletContext().getRealPath("file/1.swf"));
		System.out.println("第一步：生成文件对象，准备转换");
		//转换成pdf文件
		if(sourceFile.exists()) {
			if(!pdfFile.exists()) {
				OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
				try {
					connection.connect();
					DocumentConverter converter = new OpenOfficeDocumentConverter(connection);   
					converter.convert(sourceFile, pdfFile);
					pdfFile.createNewFile();
					connection.disconnect();  
					System.out.println("第二步：转换为PDF格式	路径" + pdfFile.getPath());
				} catch (java.net.ConnectException e) {
					e.printStackTrace();
					System.out.println("OpenOffice服务未启动");
					throw e;
				} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
					e.printStackTrace();
					System.out.println("读取文件失败");
					throw e;
				} catch (Exception e){
					e.printStackTrace();
					try {
						throw e;
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			} else {
				System.out.println("已转换为PDF，无需再次转换");
			}
		} else {
			System.out.println("要转换的文件不存在");
		} 
		//转换成swf文件
		r = Runtime.getRuntime();
		if(!swfFile.exists()){
			if(pdfFile.exists()) {
				try {
					Process p = r.exec("C:/Program Files (x86)/SWFTools/pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
					p.waitFor();
					swfFile.createNewFile();
					System.out.println("第三步：转换为SWF格式	路径：" + swfFile.getPath());
					System.out.println("第si步：转换为SWF格式mingcheng：" + swfFile.getName());
					if(pdfFile.exists()) {
						pdfFile.delete();
					}
				} catch (Exception e) {
					e.printStackTrace();
					try {
						throw e;
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			} else {
				System.out.println("PDF文件不存在，无法转换");
			}
		} else {
			System.out.println("已经转为SWF文件，无需再次转换");
		}
		HttpSession session = request.getSession();
		session.setAttribute("fileName", swfFile.getName());
		response.sendRedirect(request.getContextPath()+"/flexpaper/readFile.jsp");
	}
}
